﻿/*
 * Распознавание образа для обеих НС
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DiplomNN
{
    class Recognition
    {
        const int L1 = 2500;
        const int L2 = 200;
        static int L3 = Helper.L3;

        public static void Recognize()
        {
            string Result = "";
            Bitmap Bm = new Bitmap(Helper.ImageFromStartFormScaledBunary);
            double[] Image = BodyRecogn.ByteRGBToBynaryArr(Colors.BitmapToByteRgb(Bm));
            Bm.Dispose();
            double[] Layer2 = new double[L2];
            double[] Layer3 = new double[L3];
            double S = 0;

            for (int j = 0; j < L2; j++)
            {
                for (int i = 0; i < L1; i++)
                {
                    S += Helper.Wij[i, j] * Image[i];
                }
                S = S - Helper.Tj[j];
                Layer2[j] = (double)((1 / (1 + Math.Pow(2.718281, (-S)))));
                S = 0;
            }

            S = 0;
            for (int k = 0; k < L3; k++)//Для выходного слоя
            {
                for (int j = 0; j < L2; j++)
                {
                    S += Helper.Wjk[j, k] * Layer2[j];
                }
                S = S - Helper.Tk[k];
                Layer3[k] = (double)((1 / (1 + Math.Pow(2.718281, (-S)))));
                S = 0;
            }
            Double Max = Layer3.Max();
            for (int k = 0; k < L3; k++)
            {
                if (Layer3[k] == Max)
                {
                    Result = Helper.ArrPict[k].GetName();
                    if (Max < 0.3)
                    {
                        Result = "Неизвестный образ";
                    }
                }              
            }

            Helper.NamePictureRecognized = Result;
        }

        public static void RecognizeHopfild()
        {
            string Result = "";
            Bitmap Bm = new Bitmap(Helper.ImageFromStartFormScaledBunary);
            double[] Image = BodyRecogn.ByteRGBToBynaryArr(Colors.BitmapToByteRgb(Bm));
            Bm.Dispose();

            double summ = 0;
            int sch = 0;
            int sch_old = 0;

            bool flag = true;
            while (flag)
            {
                for (int i = 0; i < 2500; i++)
                {
                    for (int i1 = 0; i1 < 2500; i1++)
                    {
                        summ += Image[i1] * Helper.GlobalArrayNN[i1, i];
                    }
                    if (summ >= 0.0)
                    {
                        summ = 1.0;
                    }
                    else
                    {
                        summ = 0.0;
                    }
                    if (summ != Image[i])
                    {
                        sch++;
                    }
                    Image[i] = (int)summ;
                    summ = 0;
                }

                if (sch_old == sch)
                {
                    flag = false;
                }
                else
                {
                    sch_old = sch;
                }
            }
            
            for (int i = 0; i < Helper.ArrPict.Length; i++)
            {
                int err = 0;
                for (int j = 0; j < 2500; j++)
                {
                    if (Image[j] != Helper.ArrPict[i].Arr[0, j])
                    {
                        err++;
                    }
                }
                if (err < 25)
                {
                    Result = Helper.ArrPict[i].GetName();
                    break;
                }
                else
                {
                    Result = "Неизвестный образ";
                }
            }
            Helper.NamePictureRecognized = Result;

        }
    }
}
